import {getExcelKey} from "@/api/settings";
import {getLogFormatSettings} from "@/api/settings";
import {DisciplineConsts, DisciplineName} from "@/consts/Consts";
import moment from "moment";

const excelKey = getExcelKey()
const logFormatSettings = getLogFormatSettings()
let disciplineMap = undefined

export function formatLogListToExcel(_logList){
    let formattedLogList = []
    const DisciplineNameMap = new Map(DisciplineName)

    for (let log of _logList){
        let date = new Date(log.date)
        let formattedLog = {
            [excelKey.count]: log.count || -1,
            [excelKey.dungeon]: log.dungeon || '',
            [excelKey.discipline]: DisciplineNameMap.get(log.discipline) || '未定义',
            [excelKey.completed]: log.completed === true? '完成':'中途退出',
            [excelKey.description]: log.description || '',
            [excelKey.image]: log.image,
            [excelKey.date]: moment(date).format('YYYY/MM/DD'),
            [excelKey.time]: moment(date).format('HH:mm:ss')
        }
        formattedLogList.push(formattedLog)
    }

    return formattedLogList
}

export function formatLogListFromExcel(_logList) {
    let logList = []

    let disciplineList = []

    for (let item of logFormatSettings.disciplineMap){
        disciplineList.push([item[1], item[0]])
    }
    disciplineMap = new Map(disciplineList)

    _logList.forEach(item=>{
        let log = formatLogFromExcel(item)
        logList.push(log)
    })
    return logList
}


export function formatLogFromExcel(_log) {
    let log = {
        'count': _log[excelKey.count] || -1,
        'dungeon': _log[excelKey.dungeon] || '', // 副本
        'discipline': disciplineFormatter(_log[excelKey.discipline]), // 职业
        'completed': completedFormatter(_log[excelKey.completed]), // 完成情况
        'description': _log[excelKey.description] || '', // 描述
        'image': _log[excelKey.image] || '',
        'date': dateFormatter(_log[excelKey.date], _log[excelKey.time]) // 日期
    }
    return log
}

function disciplineFormatter(_discipline){
    return disciplineMap.get(_discipline) || DisciplineConsts.UNKNOWN
}

function completedFormatter(_completed){
    if (_completed === undefined){
        return true
    }
    return _completed == logFormatSettings.completed
}

function dateFormatter(date, time){
    let resultDate = new Date(0)
    if (date === undefined){
        return resultDate
    }
    let offset = (new Date()).getTimezoneOffset() * 60 * 1000

    if ((typeof date) === 'string') {
        resultDate = new Date(date)
    }else if ((typeof date) === 'number') {
        resultDate = new Date( (date - 25569) * 24 * 3600000 + offset)
    }

    if (time !== undefined && time != null && time !== '') {
        if ((typeof time) === 'string') {
            resultDate = new Date(resultDate.getFullYear() + '/'
                + (1 + resultDate.getMonth()) + '/'
                + resultDate.getDate() + ' '
                + time)
        }else if ((typeof time) === 'number') {
            resultDate = new Date( resultDate.getTime() + time * 24 * 3600000)
        }
    }


    return resultDate
}